param(
    [string]$BindHost = '127.0.0.1',
    [int]$Port = 5000,
    [switch]$Debug,
    [switch]$Check
)

$ErrorActionPreference = 'Stop'

# Project root (this script directory)
$ProjectRoot = $PSScriptRoot
Set-Location $ProjectRoot

# Detect Python (simple and compatible)
$pythonCmd = $null
if (Get-Command python -ErrorAction SilentlyContinue) { $pythonCmd = 'python' }
elseif (Get-Command py -ErrorAction SilentlyContinue) { $pythonCmd = 'py -3' }
else { Write-Error 'Python not found. Please install Python 3.'; exit 1 }

# Ensure uploads dir exists
$uploadsDir = Join-Path $ProjectRoot 'page\uploads'
if (-not (Test-Path $uploadsDir)) { New-Item -ItemType Directory -Path $uploadsDir | Out-Null }

# Build command (run src.start_api as module)
$cmd = "$pythonCmd -m src.start_api --host $BindHost --port $Port"
if ($Debug) { $cmd = "$cmd --debug" }
if ($Check) { $cmd = "$cmd --check" }

Write-Host "Command: $cmd" -ForegroundColor DarkGray
Write-Host 'Press Ctrl+C to stop the server' -ForegroundColor DarkGray

# 运行（阻塞直至退出）
Invoke-Expression $cmd